package com.weiyuwqj.server.service.reward.impl;

import com.github.pagehelper.PageHelper;
import com.weiyuwqj.server.service.account.module.DealAction;
import com.weiyuwqj.server.service.account.module.DealModule;
import com.weiyuwqj.server.service.account.module.action.user.invest.GenerateWaitDealAction;
import com.weiyuwqj.server.service.base.BaseServiceImpl;
import com.weiyuwqj.server.mapper.*;
import com.weiyuwqj.api.service.BasExperiencedGoldService;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.*;
import com.weiyuwqj.api.model.vo.BasExperiencedGoldVo;
import com.weiyuwqj.api.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huzj on 2016/8/12.
 */
@Service("experiencedGoldService")
public class BasExperiencedGoldServiceImp extends BaseServiceImpl<BasExperiencedGold> implements BasExperiencedGoldService {
    @Autowired
    private BasExperiencedGoldMapper experiencedGoldMapper;

    @Autowired
    private BusItemInvestMapper investMapper;

    @Autowired
    private DealModule dealModule;

    @Autowired
    private BasUserSecurityMapper securityMapper;

    @Autowired
    private BusAccountBankMapper accountBankMapper;

    @Autowired
    private BusItemCollectMapper itemCollectMapper;

    @Autowired
    private BusIncomeStatMapper incomeStatMapper;

    @Override
    public PageList getList(Paginator p, Map param) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        List<BasExperiencedGoldVo> list = experiencedGoldMapper.getList(param);
        return new PageList(list);
    }

    @Override
    public Map useExperiencedGold(Integer userId, Integer experiencedGoldId) {
        BasExperiencedGold experiencedGold = experiencedGoldMapper.selectByPrimaryKey(experiencedGoldId);
        if (experiencedGold == null) {
            throw new CrowdException("sys.experienced_gold.not_exist");
        }
        if (experiencedGold.getStatus() != 2) {
            throw new CrowdException("sys.experienced_gold.can_not_use");
        }
        BasUserSecurity security = securityMapper.getOneByUserId(userId);
        if (security == null || security.getRealnameStatus() != 1) {
            throw new CrowdException("sys.experienced_gold.user.not_realname");
        }
        List accountBankList = accountBankMapper.getBankByUserId(userId);
        if (accountBankList == null || accountBankList.size() == 0) {
            throw new CrowdException("sys.experienced_gold.user.not_binding_card");
        }
        BigDecimal investDealAmount = experiencedGold.getAmount();
        BigDecimal collectInterest = investDealAmount.
                multiply(experiencedGold.getRate()).
                multiply(BigDecimal.valueOf(experiencedGold.getUsefulLife())).
                divide(BigDecimal.valueOf(360), 2, BigDecimal.ROUND_HALF_EVEN);
        String investOrder = RandomCodesUtils.getOrderNo();

        BusItemInvest invest = new BusItemInvest();
        invest.setUserId(experiencedGold.getUserId());
        invest.setItemId(0);
        invest.setInvestCurrent(0);
        invest.setInvestType(6);
        invest.setInvestAmount(experiencedGold.getAmount());
        invest.setInvestOrder(investOrder);
        invest.setInvestStatus(1);
        invest.setAddip(IpUtils.get());
        invest.setAddtime(new Date());
        invest.setInvestAmount(investDealAmount);
        invest.setInvestDealAmount(investDealAmount);
        invest.setCollectInterest(collectInterest.divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_EVEN));
        invest.setCollectPrincipal(BigDecimal.ZERO);
        invest.setCollectAmount(invest.getCollectPrincipal().add(invest.getCollectInterest()));
        invest.setAdditionalRateAmount(BigDecimal.ZERO);
        invest.setActualCollectAmount(BigDecimal.ZERO);
        invest.setActualCollectInterest(BigDecimal.ZERO);
        invest.setActualCollectPrincipal(BigDecimal.ZERO);
        invest.setActualUncollectAmount(invest.getCollectPrincipal().add(invest.getCollectInterest()));
        invest.setActualUncollectInterest(invest.getCollectInterest());
        invest.setActualUncollectPrincipal(BigDecimal.ZERO);
        investMapper.insertInvest(invest);

        BusItemCollect collect = new BusItemCollect();
        collect.setAdditionalRateAmount(BigDecimal.ZERO);
        collect.setItemId(invest.getItemId());
        collect.setCollectAmount(invest.getCollectPrincipal().add(invest.getCollectInterest()));
        collect.setCollectTotalPeriod(1);
        collect.setCollectCurrentPeriod(1);
        collect.setCollectPrincipal(BigDecimal.ZERO);
        collect.setCollectInterest(invest.getCollectInterest());
        collect.setCollectTime(DateUtils.addTime(1, DateUtils.setDayZero(invest.getAddtime()), experiencedGold.getUsefulLife()));
        collect.setActualCollectAmount(BigDecimal.ZERO);
        collect.setActualCollectPrincipal(BigDecimal.ZERO);
        collect.setActualCollectInterest(BigDecimal.ZERO);
        collect.setAddip(IpUtils.get());
        collect.setAddtime(new Date());
        collect.setInvestId(invest.getId());
        collect.setInvestUserId(invest.getUserId());
        collect.setCollectStatus(0);
        itemCollectMapper.insert(collect);

        Map goldMap = new HashMap();
        goldMap.put("investId", invest.getId());
        goldMap.put("useTime", new Date());
        goldMap.put("userId", userId);
        goldMap.put("id", experiencedGoldId);
        experiencedGoldMapper.updateUsedStatus(goldMap);

        DealAction wait = new GenerateWaitDealAction();
        wait.deal(invest.getUserId(), invest.getCollectAmount(), "体验金产生待收资金" + invest.getCollectAmount() + "元");
        dealModule.dealAction(wait);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("waitIncome", invest.getCollectInterest());
        map.put("userId", experiencedGold.getUserId());
        map.put("earnedIncome", 0);
        map.put("totalIncome", invest.getCollectInterest());
        int count = incomeStatMapper.updateByWaitEarned(map);

        Map ret = new HashMap();
        ret.put("result", "success");
        ret.put("investOrder", investOrder);
        ret.put("dealAmount", investDealAmount);
        return ret;
    }


}
